package com.example.myfirstgame.model;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;

import com.example.myfirstgame.controller.GameObject;
import com.example.myfirstgame.helper.Coordinate;
import com.example.myfirstgame.helper.DataInstance;
import com.example.myfirstgame.helper.RasterPoint;
import com.example.myfirstgame.resources.Colors;

public class ParkingLot implements GameObject {
	// unique id
	private static int counter = 0;
	private int parkLotId = 0xbe89;
	private int parkedCarId = 0; 
	private int type;
	// position
	private Rect collisionRectangle = null;

	private float posX;
	private float posY;
	private boolean emptySpace = true;

	private float newWidthFl;
	private float newHeightFl;
	private float newWidthInt;
	private float newHeightInt;

	private Rect[] rectangles = null;

	private static float mInternalScaleFactor = 1.5f;
	private static Paint myPaint = new Paint();

	private static Context appContext = null;
	// Final stuff
	public static final String TYPE_DEFINITION = "parkingLot";
	public static final int TYPE_01 = 0x12d;
	public static final int TYPE_02 = TYPE_01 + 1;

	// ==================
	// CONSTRUCTOR
	// ==================

//	public ParkingLot(int _type, Context _appContext) {
//		this.type = _type;
//		scaleObject(DataInstance.getInstance().getGameObjectScalingFactor());
//		if (appContext == null) {
//			appContext = _appContext;
//		}
//		parkLotId = counter;
//		counter++;
//	}

//	public ParkingLot(int _type, Context _appContext, int x, int y) {
//		this.type = _type;
//		scaleObject(DataInstance.getInstance().getGameObjectScalingFactor());
//		if (appContext == null) {
//			appContext = _appContext;
//		}
//		posX = x;
//		posY = y;
//		parkLotId = counter;
//		counter++;
//	}
	
	public ParkingLot(int _type, Context _appContext, RasterPoint pRasterPoint ) {
		this.type = _type;
		scaleObject(DataInstance.getInstance().getGameObjectScalingFactor(), pRasterPoint.getRasterSize());
		if (appContext == null) {
			appContext = _appContext;
		}
		posX = pRasterPoint.getCoordinate().getX();
		posY = pRasterPoint.getCoordinate().getY();
		parkLotId = counter;
		counter++;
	}

	// ========================
	// METHODS
	// ========================

	public void draw(Canvas canvas) {
		myPaint.setColor(Colors.WHITE);
		Rect[] rects = buildRectangelsForDrawing();
		for (Rect r : rects) {
			canvas.drawRect(r, myPaint);
		}
		myPaint.setColor(Colors.GREY);
		canvas.drawRect(getBoundingRectangle(), myPaint);
		// myPaint.setColor(Colors.TRANS_RED);
		// canvas.drawRect(this.collisionRectangle, myPaint);
	}

	public int getParkedCarId() {
		return parkedCarId;
	}

	public void setParkedCarId(int parkedCarId) {
		this.parkedCarId = parkedCarId;
	}
	
//	public void setPosition(int x, int y) {
//		this.posX = x;
//		this.posY = y;
//	}
	
	public void setPosition(RasterPoint pRasterPoint) {
		this.posX = pRasterPoint.getCoordinate().getX();
		this.posY = pRasterPoint.getCoordinate().getY();
	}

	public int getWidth() {
		return (int) newWidthFl;
	}

	public int getHeight() {
		return (int) newHeightFl;
	}

	public boolean isEmptySpace() {
		return emptySpace;
	}

	public void setEmptySpace(boolean emptySpace) {
		this.emptySpace = emptySpace;
	}

	// ===============================
	// Interface Collision Object
	// ===============================

	@Override
	public String getCollisonType() {
		return TYPE_DEFINITION;
	}

	@Override
	public Rect getBoundingRectangle() {

		if (collisionRectangle == null) {

			newWidthInt = (int) newWidthFl;
			newHeightInt = (int) newHeightFl;

			Rect rect = new Rect((int) (posX),
					(int) (posY),
					(int) (posX + newWidthInt),
					(int) (posY + newHeightInt));
			collisionRectangle = rect;
		}

		return collisionRectangle;
	}

	@Override
	public String getGameType() {
		// TODO Auto-generated method stub
		return TYPE_DEFINITION;
	}

	@Override
	public void scaleObject(float pScaleFactor, int pRasterSize) {
		float lCurrentScaleFactor = pScaleFactor * mInternalScaleFactor;
		newHeightFl = 10 * lCurrentScaleFactor * pRasterSize;
		newWidthFl = 6 * lCurrentScaleFactor * pRasterSize;
		collisionRectangle = null;
		rectangles = null;
	}

	private Rect[] buildRectangelsForDrawing() {

		if (rectangles == null) {
			// sideline 1
			Rect[] rects = new Rect[1];
			// Rect rect1 = new Rect((int) (posX + newWidthInt / 2),
			// (int) (posY + newHeightInt / 2), (int) (posX + newWidthInt
			// / 2 - newWidthInt / 10),
			// (int) (posY - newHeightInt / 2));
			// rects[0] = rect1;
			// // sideline 2
			// Rect rect2 = new Rect((int) (posX - newWidthInt / 2),
			// (int) (posY + newHeightInt / 2), (int) (posX - newWidthInt
			// / 2 + newWidthInt / 10),
			// (int) (posY - newHeightInt / 2));
			// rects[1] = rect2;
			// // endline
			// Rect rect3 = new Rect((int) (posX - newWidthInt / 2),
			// (int) (posY - newHeightInt / 2 + newHeightInt / 20), (int) (posX
			// + newWidthInt
			// / 2),
			// (int) (posY - newHeightInt / 2));
			// rects[2] = rect3;
			//TODO daraus eine Methode formen, die Rectangles neu scalieren kann
			int centerX = getBoundingRectangle().centerX();
			int centerY = getBoundingRectangle().centerY();
			int oldHeight = getBoundingRectangle().height();
			int oldWidth = getBoundingRectangle().width();
//			int newLeft = 0;
//			int newTop = 0;
//			int newRight = 0;
//			int newBottom = 0;
//			if (centerX - 11*oldWidth/20 <= 0){newLeft = centerX - 11*oldWidth/20;}
//			if (centerY - 11*oldHeight/20 <= 0){newTop = centerY - 11*oldHeight/20;}
//			if (centerX + 11*oldWidth/20 <= 0){newRight = centerX + 11*oldWidth/20;}
//			if (centerY + 11*oldHeight/20 <= 0){newRight = centerY + 11*oldHeight/20;}
			rects[0] = new Rect(centerX - 11*oldWidth/20, centerY - 11*oldHeight/20 , centerX + 11*oldWidth/20, centerY + 11*oldHeight/20);
			rectangles = rects;
		}
		return rectangles;
	}

	@Override
	public void setPosition(Coordinate coordinate) {
		this.posX = coordinate.getX();
		this.posY = coordinate.getY();
	}

	@Override
	public Coordinate getPosition() {
		return new Coordinate(posX, posY);
	}

}
